/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.concurrent.TimeUnit;

/**
 * <p>Project: javaseapp - Ex12
 * <p>Powered by webrx On 2021-08-26 11:35:34
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 16
 */
public class Ex12 {
    void work() {
        String tn = Thread.currentThread().getName();
        for (int i = 0; i < 5; i++) {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.printf("%s = %d%n", tn, i);
        }
    }

    public static void main(String[] args) {
        var ex = new Ex12();
        var t1 = new Thread(ex::work, "T1");
        var t2 = new Thread(ex::work, "T2");
        var t3 = new Thread(ex::work, "T3");
        var t4 = new Thread(ex::work, "T4");
        //并行
        //t2->t3 串行执行
        //t1->t3 串行执行 先执行t1再执行t3
        t1.start();
        t2.start();
        try {
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t3.start();
        try {
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t4.start();
    }

    public static void test(String[] args) {
        var ex = new Ex12();
        var t1 = new Thread(ex::work, "T1");
        var t2 = new Thread(ex::work, "T2");
        var t3 = new Thread(ex::work, "T3");
        var t4 = new Thread(ex::work, "T4");

        t1.start();
        try {
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.start();
    }
}
